home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / AmigaTalk / user / FileCounter.st < prev    next >
Text File  |  2004-01-31  |  6KB  |  167 lines

  1. "--------------------------------------------------------------------------"
  2. " Count the number of files that match the regular expression given.       "
  3. " After parsing this into AmigaTalk, enter the following commands into the "
  4. " Command Line Gadget:                                                     "
  5. "    iconCounter <- FileCounter new "
  6. "    iconCounter test    or         "
  7. ""
  8. "    iconCounter main: 'yourDirectory' regEx: 'searchPattern' "
  9. ""
  10. " in order to see how to use the AmigaDOS classes.                         "
  11. "--------------------------------------------------------------------------"
  12.  
  13. Class FileCounter :Object
  14. ! safe unsafe danger dflags rmode fibType err !
  15. [
  16.    initialize
  17.       danger  <- DangerousDOS new. 
  18.       unsafe  <- UnSafeDOS    new. 
  19.       safe    <- SafeDOS      new.
  20.       dflags  <- DosSystem    new.
  21.  
  22.       rmode   <- dflags getDosFlag:  #ACCESS_READ.
  23.       fibType <- dflags getDosFlag:  #DOS_FIB.
  24.       err     <- dflags getDosError: #ERROR_NO_MORE_ENTRIES.
  25. |
  26.    countDirectoriesIn: rootLock ! fib rval tLock !
  27.       rval <- 0.
  28.       fib  <- danger allocDosObject: fibType tags: nil.
  29.        
  30.        (fib isNil)
  31.           ifTrue: [ 'Could NOT allocate a Dos Object (fib)!' print.
  32.                     ^ 0
  33.                   ].
  34.        
  35.        tLock <- unsafe duplicateLock: rootLock.
  36.  
  37.        (tLock isNil) 
  38.           ifTrue: [ danger freeDosObject: fib type: fibType.
  39.                     ('Could NOT duplicate lock!') print.  
  40.                     ^ 0
  41.                   ].
  42.           
  43.        ((unsafe examine: tLock into: fib) ~= true or: [unsafe isFileIn: fib])
  44.           ifTrue: [ unsafe unLock: tLock.
  45.                     danger freeDosObject: fib type: fibType.
  46.                     ('Please supply a Directory!') print.
  47.                     ^ 0
  48.                   ].
  49.  
  50.        [(unsafe examineNext: tLock into: fib) ~= false or: [safe getIoErr ~= err]]
  51.           whileTrue: [(unsafe isFileIn: fib)
  52.                          ifFalse: [ rval <- rval + 1 ] " Count as a Directory "
  53.                      ].
  54.  
  55.       unsafe unLock: tLock.
  56.  
  57.       danger freeDosObject: fib type: fibType.
  58.  
  59.       ^ rval
  60. |
  61.    countFilesIn: rootLock using: regularExpression ! abuffer bufsize fib rval tLock !
  62.       rval <- 0.
  63.       fib  <- danger allocDosObject: fibType tags: nil.
  64.        
  65.        (fib isNil)
  66.           ifTrue: [ 'Could NOT allocate a Dos Object (fib)!' print.
  67.                     ^ 0
  68.                   ].
  69.  
  70.        bufsize <- (regularExpression size) * 4.
  71.        abuffer <- String new: bufsize.
  72.       
  73.        ((unsafe parsePatternNoCase: regularExpression into: abuffer ofSize: bufsize) isNil)
  74.           ifTrue: [ danger freeDosObject: fib type: fibType.
  75.                     ('Could NOT parse ', regularExpression) print.
  76.                     ^ 0
  77.                   ].
  78.        
  79.        tLock <- unsafe duplicateLock: rootLock.
  80.  
  81.        (tLock isNil) 
  82.           ifTrue: [ danger freeDosObject: fib type: fibType.
  83.                     ('Could NOT duplicate lock!') print.  
  84.                     ^ 0
  85.                   ].
  86.           
  87.        ((unsafe examine: tLock into: fib) ~= true or: [unsafe isFileIn: fib])
  88.           ifTrue: [ unsafe unLock: tLock.
  89.                     danger freeDosObject: fib type: fibType.
  90.                     ('Please supply a Directory!') print.
  91.                     ^ 0
  92.                   ].
  93.  
  94.        [(unsafe examineNext: tLock into: fib) ~= false or: [safe getIoErr ~= err]]
  95.           whileTrue: [(unsafe isFileIn: fib)
  96.                          ifTrue: [(unsafe matchPatternNoCase: abuffer 
  97.                                                           in: (unsafe getFileNameFrom: fib))
  98.                                      ifTrue: [rval <- rval + 1]
  99.                                  ].
  100.                      ].
  101.  
  102.       unsafe unLock: tLock.
  103.  
  104.       danger freeDosObject: fib type: fibType.
  105.  
  106.       ^ rval
  107. |
  108.    test ! myLock howMany !
  109.       howMany <- 0.
  110.       
  111.       self initialize.
  112.       'Initialization done!' print.
  113.       
  114.       myLock  <- unsafe lockFile: 'AmigaTalk:' mode: rmode.
  115.       
  116.       (myLock isNil)
  117.          ifTrue: [ 'Could NOT lock on directory!' print.
  118.                    ^ nil
  119.                  ].
  120.          
  121.       howMany <- self countFilesIn: myLock using: '#?.info'.
  122.  
  123.       (howMany ~= 0)
  124.           ifTrue: [('There are ', (howMany asString), ' files that match  #?.info') print ]
  125.          ifFalse: [ 'There are no files that match #?.info' print ]
  126. |
  127.    matchFilesIn: myDirectory regEx: regularExpression ! howMany myLock !
  128.       howMany <- 0.
  129.       
  130.       self initialize.
  131.  
  132.       myLock  <- unsafe lockFile: myDirectory mode: rmode.
  133.  
  134.       (myLock isNil)
  135.          ifTrue: [ ('Could NOT lock on ', myDirectory, '!' ) print.
  136.                    ^ nil
  137.                  ].
  138.          
  139.       howMany <- self countFilesIn: myLock using: regularExpression.
  140.       
  141.       (howMany ~= 0)
  142.           ifTrue: [('There are ', (howMany asString), ' files that match ',
  143.                                   regularExpression) print
  144.                   ]
  145.          ifFalse: [('There are no files that match', regularExpression) print]
  146. |
  147.    countDirectories: myRootDirectory ! howMany myLock !
  148.       howMany <- 0.
  149.       
  150.       self initialize.
  151.  
  152.       myLock  <- unsafe lockFile: myDirectory mode: rmode.
  153.  
  154.       (myLock isNil)
  155.          ifTrue: [ ('Could NOT lock on ', myDirectory, '!' ) print.
  156.                    ^ nil
  157.                  ].
  158.          
  159.       howMany <- self countDirectoriesIn: rooLock
  160.  
  161.       (howMany ~= 0)
  162.           ifTrue: [('There are ', (howMany asString), ' Directoriesfiles in ',
  163.                                   myRooDirectory) print
  164.                   ]
  165.          ifFalse: [('There are no Directories in ', myRootDirectory) print]
  166. ]
  167.